﻿using d1Common;
using d1Common.WebController;
using d1Project.Modules.Common.do4A.Models;
using d1Project.Tools;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace d1Project.Modules.Common.do4A.Domain
{
    public class AuthenticationMgr
    {
        private static AuthenticationMgr singleInstance = new AuthenticationMgr();
        public static AuthenticationMgr Instance
        {
            get
            {
                return singleInstance;
            }
        }
        public AuthenticationMgr()
        {
            this.Token0_ServiceToken = doServices.Instance.DoConfig.readConfigStringValue(do4AModule.ModuleIdDefine, "Token0", "ServiceToken", "");

            this.Token1_defaultPassword = TextHelper.GetMd5(doServices.Instance.DoConfig.readConfigStringValue(do4AModule.ModuleIdDefine, "Token1", "defaultPassword", "123456"));
            this.Token1_tokenDuration = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token1", "tokenDuration", -1);
            this.Token1_maxPasswordErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token1", "maxPasswordErrorCount", -1);
            this.Token1_lockSpanForPsswordError = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token1", "lockSpanForPsswordError", 720);
            this.Token1_permissionCheck = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token1", "permissionCheck", true);
            this.Token1_onlyOneLogin = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token1", "onlyOneLogin", false);
            this.Token1_useVerficationCode = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token1", "useVerficationCode", false);
            this.Token1_VerficationLength = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token1", "VerficationLength", 4);
            this.Token1_VerficationSpan = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token1", "VerficationSpan", 10);
            this.Token1_VerficationMaxErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token1", "VerficationMaxErrorCount", 10);

            this.Token2_defaultPassword = TextHelper.GetMd5(doServices.Instance.DoConfig.readConfigStringValue(do4AModule.ModuleIdDefine, "Token2", "defaultPassword", "123456"));
            this.Token2_tokenDuration = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token2", "tokenDuration", -1);
            this.Token2_maxPasswordErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token2", "maxPasswordErrorCount", -1);
            this.Token2_lockSpanForPsswordError = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token2", "lockSpanForPsswordError", 720);
            this.Token2_onlyOneLogin = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token2", "onlyOneLogin", true);
            this.Token2_useVerficationCode = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token2", "useVerficationCode", false);
            this.Token2_VerficationLength = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token2", "VerficationLength", 4);
            this.Token2_VerficationSpan = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token2", "VerficationSpan", 10);
            this.Token2_VerficationMaxErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token2", "VerficationMaxErrorCount", 10);

            this.Token3_defaultPassword = TextHelper.GetMd5(doServices.Instance.DoConfig.readConfigStringValue(do4AModule.ModuleIdDefine, "Token3", "defaultPassword", "123456"));
            this.Token3_tokenDuration = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token3", "tokenDuration", -1);
            this.Token3_maxPasswordErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token3", "maxPasswordErrorCount", -1);
            this.Token3_lockSpanForPsswordError = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token3", "lockSpanForPsswordError", 720);
            this.Token3_onlyOneLogin = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token3", "onlyOneLogin", false);
            this.Token3_useVerficationCode = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token3", "useVerficationCode", false);
            this.Token3_VerficationLength = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token3", "VerficationLength", 4);
            this.Token3_VerficationSpan = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token3", "VerficationSpan", 10);
            this.Token3_VerficationMaxErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token3", "VerficationMaxErrorCount", 10);

            this.Token4_defaultPassword = TextHelper.GetMd5(doServices.Instance.DoConfig.readConfigStringValue(do4AModule.ModuleIdDefine, "Token4", "defaultPassword", "123456"));
            this.Token4_tokenDuration = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token4", "tokenDuration", -1);
            this.Token4_maxPasswordErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token4", "maxPasswordErrorCount", -1);
            this.Token4_lockSpanForPsswordError = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token4", "lockSpanForPsswordError", 720);
            this.Token4_onlyOneLogin = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token4", "onlyOneLogin", false);
            this.Token4_useVerficationCode = doServices.Instance.DoConfig.readConfigBoolValue(do4AModule.ModuleIdDefine, "Token4", "useVerficationCode", false);
            this.Token4_VerficationLength = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token4", "VerficationLength", 4);
            this.Token4_VerficationSpan = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token4", "VerficationSpan", 10);
            this.Token4_VerficationMaxErrorCount = doServices.Instance.DoConfig.readConfigIntegerValue(do4AModule.ModuleIdDefine, "Token4", "VerficationMaxErrorCount", 10);
        }

        public string Token0_ServiceToken { get; private set; }
        public string Token1_defaultPassword { get; private set; }
        public int Token1_tokenDuration { get; private set; }
        public int Token1_maxPasswordErrorCount { get; private set; }
        public int Token1_lockSpanForPsswordError { get; private set; }
        public bool Token1_permissionCheck { get; private set; }
        public bool Token1_onlyOneLogin { get; private set; }
        public bool Token1_useVerficationCode { get; private set; }
        public int Token1_VerficationLength { get; private set; }
        public int Token1_VerficationSpan { get; private set; }
        public int Token1_VerficationMaxErrorCount { get; private set; }

        public string Token2_defaultPassword { get; private set; }
        public int Token2_tokenDuration { get; private set; }
        public int Token2_maxPasswordErrorCount { get; private set; }
        public int Token2_lockSpanForPsswordError { get; private set; }
        public bool Token2_onlyOneLogin { get; private set; }
        public bool Token2_useVerficationCode { get; private set; }
        public int Token2_VerficationLength { get; private set; }
        public int Token2_VerficationSpan { get; private set; }
        public int Token2_VerficationMaxErrorCount { get; private set; }

        public string Token3_defaultPassword { get; private set; }
        public int Token3_tokenDuration { get; private set; }
        public int Token3_maxPasswordErrorCount { get; private set; }
        public int Token3_lockSpanForPsswordError { get; private set; }
        public bool Token3_onlyOneLogin { get; private set; }
        public bool Token3_useVerficationCode { get; private set; }
        public int Token3_VerficationLength { get; private set; }
        public int Token3_VerficationSpan { get; private set; }
        public int Token3_VerficationMaxErrorCount { get; private set; }

        public string Token4_defaultPassword { get; private set; }
        public int Token4_tokenDuration { get; private set; }
        public int Token4_maxPasswordErrorCount { get; private set; }
        public int Token4_lockSpanForPsswordError { get; private set; }
        public bool Token4_onlyOneLogin { get; private set; }
        public bool Token4_useVerficationCode { get; private set; }
        public int Token4_VerficationLength { get; private set; }
        public int Token4_VerficationSpan { get; private set; }
        public int Token4_VerficationMaxErrorCount { get; private set; }

        class AuthFailRecord
        {
            public AuthFailRecord()
            {
                this.FailCount = 1;
                this.LastFailTime = DateTime.Now;
            }
            public int FailCount;
            public DateTime LastFailTime;
        }
        private Dictionary<string, AuthFailRecord> user1GetTokenFailRecords = new Dictionary<string, AuthFailRecord>();
        private Dictionary<string, AuthFailRecord> user2GetTokenFailRecords = new Dictionary<string, AuthFailRecord>();
        private Dictionary<string, AuthFailRecord> user3GetTokenFailRecords = new Dictionary<string, AuthFailRecord>();
        private Dictionary<string, AuthFailRecord> user4GetTokenFailRecords = new Dictionary<string, AuthFailRecord>();

        public string CheckToken1(string id, string pwd)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                //检查用户编码和手机号
                var user1 = db.User1s.Where(x => (x.Code == id || x.MobilePhone == id || x.Email == id)).FirstOrDefault();
                if (user1 == null) throw new UserException("用户不存在");
                if (this.Token1_maxPasswordErrorCount >= 0 && user1GetTokenFailRecords.ContainsKey(user1.Id))
                {
                    if (user1GetTokenFailRecords[user1.Id].FailCount > this.Token1_maxPasswordErrorCount)
                    {
                        if (user1GetTokenFailRecords[user1.Id].LastFailTime.AddMinutes(this.Token1_lockSpanForPsswordError) < DateTime.Now)
                        {
                            AuditLogMgr.Instance.WriteAuditLog("User1Login", "系统用户登录认证", user1.Id, user1.Name, "密码错误次数太多，账户已被临时冻结");
                            throw new UserException("密码错误次数太多，账户已被临时冻结！");
                        }
                        else
                        {
                            user1GetTokenFailRecords.Remove(user1.Id);
                        }
                    }
                }
                var userPwd = db.User1Pwds.Where(x => x.Id == user1.Id).FirstOrDefault();
                bool _isPassed = false;
                if (userPwd == null)
                {
                    //对比默认密码
                    if (this.Token1_defaultPassword == pwd) _isPassed = true;
                }
                else
                {
                    if (userPwd.Password == pwd) _isPassed = true;
                }
                if (!_isPassed)
                {
                    if (this.Token1_maxPasswordErrorCount >= 0)
                    {
                        if (user1GetTokenFailRecords.ContainsKey(user1.Id))
                        {
                            user1GetTokenFailRecords[user1.Id].FailCount++;
                            user1GetTokenFailRecords[user1.Id].LastFailTime = DateTime.Now;
                        }
                        else
                        {
                            user1GetTokenFailRecords[user1.Id] = new AuthFailRecord();
                        }
                    }
                    AuditLogMgr.Instance.WriteAuditLog("User1Login", "系统用户登录认证", user1.Id, user1.Name, "用户名或密码错误");
                    throw new UserException("用户名或密码错误");
                }
                AuditLogMgr.Instance.WriteAuditLog("User1Login", "系统用户登录认证", user1.Id, user1.Name, "验证成功");
                return this.CreateToken1(user1, "http");
            }
            finally
            {
                db.Dispose();
            }

        }
        private string CreateToken1(User1 user1, string _authType)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                if (this.Token1_onlyOneLogin)
                {
                    if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupUser1Token, user1.Id))
                    {
                        Token _tokenUser = doServices.Instance.DoCache.GetData<Token>(do4AModule.CacheGroupUser1Token, user1.Id);
                        doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken1, _tokenUser.Id);
                    }
                }
                Token token1 = new Token();
                token1.Id = doServices.Instance.DoCore.getGUID();
                token1.AuthType = _authType;
                token1.UserId = user1.Id;
                token1.UserName = user1.Name;
                token1.RoleId = user1.RoleId;
                token1.DeptId = user1.DeptId;
                token1.AreaId = user1.AreaId;
                if (this.Token1_tokenDuration < 0)
                {
                    token1.ExpiredTime = DateTime.MaxValue;
                }
                else
                {
                    token1.ExpiredTime = DateTime.Now.AddMinutes(this.Token1_tokenDuration);
                }
                doServices.Instance.DoCache.SetData(do4AModule.CacheGroupToken1, token1.Id, token1, this.Token1_tokenDuration * 60);
                return token1.Id;
            }
            finally
            {
                db.Dispose();
            }
        }

        public string CheckToken2(string id, string pwd)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                //检查手机号
                var user2 = db.User2s.Where(x => (x.Code == id || x.MobilePhone == id || x.Email == id)).FirstOrDefault();
                if (user2 == null) throw new UserException("用户不存在");
                if (this.Token2_maxPasswordErrorCount >= 0 && user2GetTokenFailRecords.ContainsKey(user2.Id))
                {
                    if (user2GetTokenFailRecords[user2.Id].FailCount > this.Token2_maxPasswordErrorCount)
                    {
                        if (user2GetTokenFailRecords[user2.Id].LastFailTime.AddMinutes(this.Token2_lockSpanForPsswordError) < DateTime.Now)
                        {
                            AuditLogMgr.Instance.WriteAuditLog("User2Login", "外部用户登录认证", user2.Id, user2.Name, "密码错误次数太多，账户已被临时冻结");
                            throw new UserException("密码错误次数太多，账户已被临时冻结！");
                        }
                        else
                        {
                            user2GetTokenFailRecords.Remove(user2.Id);
                        }
                    }
                }
                var userPwd = db.User2Pwds.Where(x => x.Id == user2.Id).FirstOrDefault();
                bool _isPassed = false;
                if (userPwd == null)
                {
                    //对比默认密码
                    if (this.Token2_defaultPassword == pwd) _isPassed = true;
                }
                else
                {
                    if (userPwd.Password == pwd) _isPassed = true;
                }
                if (!_isPassed)
                {
                    if (this.Token2_maxPasswordErrorCount >= 0)
                    {
                        if (user2GetTokenFailRecords.ContainsKey(user2.Id))
                        {
                            user2GetTokenFailRecords[user2.Id].FailCount++;
                            user2GetTokenFailRecords[user2.Id].LastFailTime = DateTime.Now;
                        }
                        else
                        {
                            user2GetTokenFailRecords[user2.Id] = new AuthFailRecord();
                        }
                    }
                    AuditLogMgr.Instance.WriteAuditLog("User2Login", "外部用户登录认证", user2.Id, user2.Name, "用户名或密码错误");
                    throw new UserException("用户名或密码错误");
                }

                AuditLogMgr.Instance.WriteAuditLog("User2Login", "系统用户登录认证", user2.Id, user2.Name, "验证成功");
                return this.CreateToken2(user2, "http");
            }
            finally
            {
                db.Dispose();
            }
        }
        public string CheckThirdPartToken2(string _loginType, string _userId)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                //检查手机号
                var user2 = db.User2s.Find(_userId);
                if (user2 == null) throw new UserException("用户不存在");
                AuditLogMgr.Instance.WriteAuditLog("thirdPartLogin", "第三方登录-" + _loginType, _userId, "", "验证成功");
                return this.CreateToken2(user2, _loginType);
            }
            finally
            {
                db.Dispose();
            }
        }
        private string CreateToken2(User2 user2, string _authType)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                if (this.Token2_onlyOneLogin)
                {
                    if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupUser2Token, user2.Id))
                    {
                        Token _tokenUser = doServices.Instance.DoCache.GetData<Token>(do4AModule.CacheGroupUser2Token, user2.Id);
                        doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken2, _tokenUser.Id);
                    }
                }
                Token token2 = new Token();
                token2.Id = doServices.Instance.DoCore.getGUID();
                token2.AuthType = _authType;
                token2.UserId = user2.Id;
                token2.UserName = user2.Name;
                if (this.Token2_tokenDuration < 0)
                {
                    token2.ExpiredTime = DateTime.MaxValue;
                }
                else
                {
                    token2.ExpiredTime = DateTime.Now.AddMinutes(this.Token2_tokenDuration);
                }

                doServices.Instance.DoCache.SetData(do4AModule.CacheGroupToken2, token2.Id, token2, this.Token2_tokenDuration * 60);
                return token2.Id;
            }
            finally
            {
                db.Dispose();
            }
        }

        public string CheckToken3(string id, string pwd)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                var _user3 = db.User3s.Where(t => (t.Code == id || t.Email == id || t.MobilePhone == id)).FirstOrDefault();
                if (_user3 == null) throw new UserException("用户不存在");
                if (this.Token3_maxPasswordErrorCount >= 0 && user3GetTokenFailRecords.ContainsKey(_user3.Id))
                {
                    if (user3GetTokenFailRecords[_user3.Id].FailCount > this.Token3_maxPasswordErrorCount)
                    {
                        if (user3GetTokenFailRecords[_user3.Id].LastFailTime.AddMinutes(this.Token3_lockSpanForPsswordError) < DateTime.Now)
                        {
                            throw new UserException("密码错误次数太多，账户已被临时冻结！");
                        }
                        else
                        {
                            user3GetTokenFailRecords.Remove(_user3.Id);
                        }
                    }
                }
                var userPwd = db.User3Pwds.Where(x => x.Id == _user3.Id).FirstOrDefault();
                bool _isPassed = false;
                if (userPwd == null)
                {
                    //对比默认密码
                    if (this.Token3_defaultPassword == pwd) _isPassed = true;
                }
                else
                {
                    if (userPwd.Password == pwd) _isPassed = true;
                }
                if (!_isPassed)
                {
                    if (this.Token3_maxPasswordErrorCount >= 0)
                    {
                        if (user3GetTokenFailRecords.ContainsKey(_user3.Id))
                        {
                            user3GetTokenFailRecords[_user3.Id].FailCount++;
                            user3GetTokenFailRecords[_user3.Id].LastFailTime = DateTime.Now;
                        }
                        else
                        {
                            user3GetTokenFailRecords[_user3.Id] = new AuthFailRecord();
                        }
                    }
                    throw new Exception("用户名或密码错误");
                }
                return this.CreateToken3(_user3, "http");
            }
            finally
            {
                db.Dispose();
            }

        }
        private string CreateToken3(User3 _user3, string _authType)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                if (this.Token3_onlyOneLogin)
                {
                    if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupUser3Token, _user3.Id))
                    {
                        Token _tokenUser = doServices.Instance.DoCache.GetData<Token>(do4AModule.CacheGroupUser3Token, _user3.Id);
                        doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken3, _user3.Id);
                    }
                }
                Token token3 = new Token();
                token3.Id = doServices.Instance.DoCore.getGUID();
                token3.AuthType = _authType;
                token3.UserId = _user3.Id;
                token3.UserName = _user3.Name;
                if (this.Token3_tokenDuration < 0)
                {
                    token3.ExpiredTime = DateTime.MaxValue;
                }
                else
                {
                    token3.ExpiredTime = DateTime.Now.AddMinutes(this.Token3_tokenDuration);
                }
                doServices.Instance.DoCache.SetData(do4AModule.CacheGroupToken3, token3.Id, token3, this.Token3_tokenDuration * 60);
                return token3.Id;
            }
            finally
            {

                db.Dispose();
            }
        }

        public string CheckToken4(string id, string pwd)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                var _user4 = db.User4s.Where(t => (t.Code == id || t.Email == id || t.MobilePhone == id)).FirstOrDefault();
                if (_user4 == null) throw new UserException("用户不存在");
                if (this.Token4_maxPasswordErrorCount >= 0 && user4GetTokenFailRecords.ContainsKey(_user4.Id))
                {
                    if (user4GetTokenFailRecords[_user4.Id].FailCount > this.Token4_maxPasswordErrorCount)
                    {
                        if (user4GetTokenFailRecords[_user4.Id].LastFailTime.AddMinutes(this.Token4_lockSpanForPsswordError) < DateTime.Now)
                        {
                            throw new UserException("密码错误次数太多，账户已被临时冻结！");
                        }
                        else
                        {
                            user4GetTokenFailRecords.Remove(_user4.Id);
                        }
                    }
                }
                var userPwd = db.User4Pwds.Where(t => t.Id == _user4.Id).FirstOrDefault();
                bool _isPassed = false;
                if (userPwd == null)
                {
                    //对比默认密码
                    if (this.Token4_defaultPassword == pwd) _isPassed = true;
                }
                else
                {
                    if (userPwd.Password == pwd) _isPassed = true;
                }
                if (!_isPassed)
                {
                    if (this.Token4_maxPasswordErrorCount >= 0)
                    {
                        if (user4GetTokenFailRecords.ContainsKey(_user4.Id))
                        {
                            user4GetTokenFailRecords[_user4.Id].FailCount++;
                            user4GetTokenFailRecords[_user4.Id].LastFailTime = DateTime.Now;
                        }
                        else
                        {
                            user4GetTokenFailRecords[_user4.Id] = new AuthFailRecord();
                        }
                    }
                    throw new Exception("用户名或密码错误");
                }
                return this.CreateToken4(_user4, "http");
            }
            finally
            {
                db.Dispose();
            }

        }
        private string CreateToken4(User4 _user4, string _authType)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                if (this.Token4_onlyOneLogin)
                {
                    if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupUser4Token, _user4.Id))
                    {
                        Token _tokenUser = doServices.Instance.DoCache.GetData<Token>(do4AModule.CacheGroupUser4Token, _user4.Id);
                        doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken4, _user4.Id);
                    }
                }
                Token token4 = new Token();
                token4.Id = doServices.Instance.DoCore.getGUID();
                token4.AuthType = _authType;
                token4.UserId = _user4.Id;
                token4.UserName = _user4.Name;
                if (this.Token4_tokenDuration < 0)
                {
                    token4.ExpiredTime = DateTime.MaxValue;
                }
                else
                {
                    token4.ExpiredTime = DateTime.Now.AddMinutes(this.Token4_tokenDuration);
                }
                doServices.Instance.DoCache.SetData(do4AModule.CacheGroupToken4, token4.Id, token4, this.Token4_tokenDuration * 60);
                return token4.Id;
            }
            finally
            {

                db.Dispose();
            }
        }


        public void RemoveToken1(string _tokenValue)
        {
            if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupToken1, _tokenValue))
            {
                doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken1, _tokenValue);
            }
        }

        public void RemoveToken2(string _tokenValue)
        {
            if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupToken2, _tokenValue))
            {
                doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken2, _tokenValue);
            }
        }

        public void RemoveToken3(string _tokenValue)
        {
            if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupToken3, _tokenValue))
            {
                doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken3, _tokenValue);
            }
        }

        public void RemoveToken4(string _tokenValue)
        {
            if (doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupToken4, _tokenValue))
            {
                doServices.Instance.DoCache.RemoveData(do4AModule.CacheGroupToken4, _tokenValue);
            }
        }

        /// <summary>
        /// 获取拼音吗
        /// </summary>
        /// <param name="str">用户姓名</param>
        /// <returns></returns>
        public string GetUserNamePinYin(string str)
        {
            string PYstr = "";
            foreach (char item in str.ToCharArray())
            {
                if (Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(item))
                {
                    Microsoft.International.Converters.PinYinConverter.ChineseChar cc = new Microsoft.International.Converters.PinYinConverter.ChineseChar(item);

                    PYstr += cc.Pinyins[0].Substring(0, cc.Pinyins[0].Length - 1);
                }
                else
                {
                    PYstr += item.ToString();
                }
            }
            return PYstr;
        }
    }

}